package com.net.oil.oilWear.model;

import com.net.oil.bean.BaseBean;
import com.net.oil.base.BaseModel;
import com.net.oil.http.AppHttpClient;
import com.net.oil.http.custom.BaseObserver;
import com.net.oil.oilWear.bean.OilWearChartBean;
import com.net.oil.oilWear.bean.OilWearInfo;
import com.net.oil.oilWear.contract.OilWearContract;
import com.net.oil.oilWear.presenter.OilWearPresenter;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：lch
 * 时间：2019/1/20 0020:下午 13:37
 * 邮箱：245850413@qq.com
 * 说明：
 */
public class OilWearModel extends BaseModel implements OilWearContract.Model {

    private OilWearPresenter presenter;

    public OilWearModel(OilWearPresenter presenter) {
        this.presenter = presenter;
        compositeDisposable = new CompositeDisposable();
    }

    @Override
    public void getOilWearInfo(int carId) {
        AppHttpClient.ApiService().getOilWearInfo(carId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<OilWearInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean<OilWearInfo> tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null && tResultBean.getData() != null){
                            presenter.getOilWearInfoSuccess(tResultBean.getData());
                        }else {
                            presenter.getOilWearInfoFailed();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        presenter.getOilWearInfoFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    @Override
    public void getOilWearChartData(int carId, int selectId) {
        AppHttpClient.ApiService().getOilWearChartData(carId, selectId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<List<OilWearChartBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean<List<OilWearChartBean>> tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null && tResultBean.getData() != null){
                            presenter.getOilWearChartDataSuccess(tResultBean.getData());
                        }else {
                            presenter.getOilWearChartDataFailed();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        presenter.getOilWearChartDataFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }
}
